﻿
// Calendar Reminder
function viewCalendarReminder() {    
    // Ajax call function
    // Handle reponse
    var dataPost = { "userId": UserLoginId };
    $.ajax({
        type: "POST",
        url: "/Lead/GetCalendarReminder",
        data: dataPost,
        dataType: "json",
        success: function (model, textStatus, jqXHR) {
            if (model != undefined && model != null && model.Title != null && model.Title != "") {
                $('#event_title').text(model.Title);
                $('#event_date').text(model.StartDate);
                $('#event_time').text(model.StartTime);
                $('#event_id').text(model.Id);
                $('#event_name').html(model.LeadName);
                $('#calendarRemind').showAlert({
                    width: 380
                });
            }
        },
        error: function (xhr, status, error) {
            // Stop progress bar
            // closePopupError();
        }
    });
}


// Calendar Reminder
function dismissCalendarReminder() {
    var dataPost = { "id": $('#event_id').text() };

    $('#calendarRemind').parents('.ui-dialog').hide();

    // Ajax call function
    // Handle reponse
    $.ajax({
        type: "POST",
        url: "/Lead/DimissCalendarReminder",
        data: dataPost,
        dataType: "json",
        success: function (model, textStatus, jqXHR) {
            // Stop progress bar
            closePopupError();
        },
        error: function (xhr, status, error) {
            //closePopupError();
        }
    });
}

// New lead notification
function viewLeadNotification() {
    var dataPost = { "userId": UserLoginId };    
    // Ajax call function
    // Handle reponse
    $.ajax({
        type: "POST",
        url: "/Lead/Notification",
        data: dataPost,
        dataType: "json",
        success: function (model, textStatus, jqXHR) {
            // Stop progress bar
            if (model != "" && model != "error") {
                $('#viewLeadLink').attr("href", "/Lead/Edit/" + model + "?query=true");
                //$('#fancybox-overlay').show();
                $('#leadNotification').showAlert({
                    width: 330
                });
            }
        },
        error: function (xhr, status, error) {
            //closePopupError();
        }
    });
}

function notificationIgnore() {
    var dataPost = {"userId": UserLoginId};

    // Close popup
    // Ajax call function
    // Handle reponse
    $.ajax({
        type: "POST",
        url: "/Lead/IgnoreNotification",
        data: dataPost,
        dataType: "json",
        success: function (data, textStatus, jqXHR) {
            hideProcess();
            closePopup();
            $('#leadNotification').parents('.ui-dialog').hide();
            $('#leadNotification').hide();
            if (data != "") {
                showPopupError();
            }
        },
        error: function (xhr, status, error) {
            // Stop progress bar
        }
    });
}


function InitNotification() {
    viewLeadNotification();
    setTimeout("viewCalendarReminder()", 3600);
}

// Set auto request
// Each request in 2 minutes
var setIntervalNewLead = setInterval("viewLeadNotification()", 120000);
var setIntervalRemind = setInterval("viewCalendarReminder()", 170000);

window.onload = InitNotification;